package club.pinea.utils;

import sun.misc.BASE64Encoder;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

/**
 * @author pineapple
 * @date 2017年12月21日 下午3:07:43
 * @description 字符串操作帮助类
 */
public class StringUtils {


    /**
     * 密码加密 password + token
     *
     * @param str
     * @return
     * @throws NoSuchAlgorithmException
     * @throws Exception
     */
    public static String encode(String str) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] b = str.getBytes();
        byte[] encode = md.digest(b);
        return new BASE64Encoder().encode(encode);
    }

    /**
     * 将IP地址加端口号过滤，只获取ip部分
     *
     * @param remoteIp channel获得的IP地址
     * @return 返回纯ipv4地址的字符串表示形式
     */
    public static String getIpFromRemoteIp(String remoteIp) {
        return remoteIp.substring(1, remoteIp.indexOf(":"));
    }

    /**
     * 返回随机的UUID
     *
     * @return
     */
    public static String getRandomUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static String getToken4User() {
        String uid = UUID.randomUUID().toString().replace("-", "");
        return uid.substring(0, 6);
    }

    public static void main(String[] args) throws NoSuchAlgorithmException {
        System.out.println(encode("123456" + "6b9f55ffcbdd4b07ba58475a903d18bd"));
    }

}
